package com.meta.act.rest.controller.tenant;

import com.github.pagehelper.PageHelper;
import com.meta.act.app.tenant.req.*;
import com.meta.act.entity.SysDept;
import com.meta.framework.common.annotation.Log;
import com.meta.framework.common.core.controller.BaseController;
import com.meta.framework.common.core.page.TableDataInfo;
import com.meta.framework.core.CommonRes;
import com.meta.framework.common.enums.BusinessType;
import com.meta.act.app.service.ISysDeptService;
import com.meta.act.app.service.TenantAppService;
import com.meta.act.app.service.TenantQryService;
import com.meta.act.app.tenant.entity.TenantBase;
import com.meta.act.app.tenant.res.TenantDetailIo;
import com.meta.act.app.tenant.res.TenantPageIo;
import com.meta.act.app.tenant.res.TenantUserVo;
import com.meta.framework.util.PageLocalHepler;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(TenantController.class);
    @Resource
    private TenantQryService tenantQryService;
    /*@Resource
    private TenantUserQryService tenantUserQryService;*/
    @Resource
    private TenantAppService tenantAppService;
    @Resource
    private ISysDeptService deptService;

    @GetMapping("/queryTenants")
    @ApiOperation(value = "查询租户列表", notes = "查询租户列表")
    @Log(title = "查询租户列表", businessType = BusinessType.QUERY)
    public CommonRes<List<TenantBase>> queryTenants(QueryTenantQry qry) {

        CommonRes<List<TenantBase>> response = new CommonRes<List<TenantBase>>();
        try {
            List<TenantBase> list = tenantQryService.listTenantUserByUserId(qry.getUserId());
            return CommonRes.success(list);
        } catch (Exception e) {
            LOG.error("queryTenants发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @GetMapping("/queryTenantOrg")
    @ApiOperation(value = "查询租户部门", notes = "查询租户部门")
    @Log(title = "查询租户部门", businessType = BusinessType.QUERY)
    public CommonRes<List<SysDept>> queryTenantOrg(Long tenantId) {
        CommonRes<List<SysDept>> response = new CommonRes<List<SysDept>>();
        try {
            SysDept dept = new SysDept();
            dept.setParentId(0L);
            dept.setTenantId(tenantId);
            List<SysDept>  orgList = deptService.selectDeptList(dept);
            return CommonRes.success(orgList);
        } catch (Exception e) {
            LOG.error("queryTenantOrg发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @GetMapping("/userList")
    @ApiOperation(value = "查询租户用户列表", notes = "查询租户用户列表")
    @Log(title = "查询租户用户列表", businessType = BusinessType.QUERY)
    public CommonRes<List<TenantUserVo>> userList(TenantUserListQry qry) {
        CommonRes<List<TenantUserVo>> response = new CommonRes<List<TenantUserVo>>();
        try {
            /*List<TenantUserVo> list = tenantUserQryService.listTenantUser(qry);
            return CommonRes.success(list);*/
            return null;
        } catch (Exception e) {
            LOG.error("queryTenants发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询租户", notes = "分页查询租户")
    @Log(title = "分页查询租户", businessType = BusinessType.QUERY)
    public TableDataInfo list(TenantBase tenantBase)
    {
        startPage();
        PageLocalHepler.setPageLocal(PageHelper.getLocalPage());
        TableDataInfo<TenantPageIo> res = getDataTable(tenantQryService.listTenantBase(tenantBase));
        res.setTotal(PageLocalHepler.getTotal());
        PageLocalHepler.clear();
        return res;
    }

    @GetMapping("/detail")
    @ApiOperation(value = "租户详情查询", notes = "租户详情查询")
    @Log(title = "租户详情查询", businessType = BusinessType.QUERY)
    public CommonRes<TenantDetailIo> detail(TenantDetailQry qry) {
        try {
            return CommonRes.success(tenantQryService.detail(qry));
        } catch (Exception e) {
            LOG.error("detail发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存租户", notes = "保存租户")
    @Log(title = "保存租户", businessType = BusinessType.INSERT)
    public CommonRes<Long> save(@RequestBody TenantCreateCmd cmd) {
        try {
            Long tenantId = tenantAppService.save(cmd);
            if(tenantId == null){
                return CommonRes.error();
            }else {
                return CommonRes.success(tenantId);
            }
        } catch (Exception e) {
            LOG.error("save发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑租户", notes = "编辑租户")
    @Log(title = "编辑租户", businessType = BusinessType.UPDATE)
    public CommonRes<Integer> edit(@RequestBody TenantEditCmd cmd) {
        try {
            tenantAppService.edit(cmd);
            return CommonRes.success();
        } catch (Exception e) {
            LOG.error("edit发生异常:{}", e);
            return CommonRes.error();
        }
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    @Log(title = "添加用户", businessType = BusinessType.INSERT)
    public CommonRes<Integer> addUser(@RequestBody TenantAddUserCmd cmd) {

        CommonRes<Integer> response = new CommonRes<Integer>();
        try {
            tenantAppService.addUser(cmd);
        } catch (Exception e) {
            LOG.error("edit发生异常:{}", e);
            return CommonRes.error();
        }

        return response;
    }

    @PutMapping("/editUser")
    @ApiOperation(value = "编辑用户", notes = "编辑用户")
    @Log(title = "编辑用户", businessType = BusinessType.UPDATE)
    public CommonRes<Integer> editUser(@RequestBody TenantEditUserCmd cmd) {
        try {
            tenantAppService.editUser(cmd);
            return CommonRes.success();
        } catch (Exception e) {
            LOG.error("edit发生异常:{}", e);
            return CommonRes.error();
        }
    }
}
